﻿
///////////////////////////////////////////////////////////
//This code is taken from the Channel9 video tutorials.
//They are here for convenience - check the web link in the
//folder for each of these to see the original postings.
//
//These challenges are completed - the uncompleted ones are in
//a subfolder below this one, named OriginalCode
//////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Subjects;
using System.Text;

namespace IntroductionToRx
{
    class MyEvents
    {
        public event Action<string> TextChanged;
        private event Action<int> lenChanged;

        private int lastLen;

        public virtual void OnTextChanged(string text)
        {
            //avoid race conditions
            var t = TextChanged;
            if (t != null) t(text);

            //check for length change
            if (lastLen != text.Length)
            {
                lastLen = text.Length;
                OnLenChanged(text.Length);
            }
        }
        public virtual void OnLenChanged(int len)
        {
            var t = lenChanged;
            if (t != null) t(len);
        }

        public event Action<int> LengthChanged
        {
            add
            {
                lenChanged = (Action<int>)Delegate.Combine(lenChanged, value);
            }
            remove
            {
                lenChanged = (Action<int>)Delegate.Remove(lenChanged, value);
            }
        }
    }
}
